package com.itmk.colloge.controller;

import com.alibaba.fastjson.JSONObject;
import com.itmk.colloge.entity.Colloge;
import com.itmk.colloge.entity.CollogeParm;
import com.itmk.colloge.entity.TableData;
import com.itmk.colloge.service.CollogeService;
import com.itmk.colloge.service.impl.CollogeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.UUID;

///colloge.do的请求，都会被该servlet处理
@WebServlet("/colloge.do")
public class CollogeServlet extends HttpServlet {
    CollogeService collogeService = new CollogeServiceImpl();

    private static final String PREX = "WEB-INF/jsp/admin";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (method.equals("collogeUI")) {
            collogeUI(req, resp);
        } else if (method.equals("addUI")) {
            addCollogeUI(req, resp);
        } else if (method.equals("addSave")) {
            addSave(req, resp);
        } else if (method.equals("list")) {
            list(req, resp);
        } else if (method.equals("editUI")) {
            editUI(req, resp);
        } else if (method.equals("editSave")) {
            editSave(req, resp);
        } else if (method.equals("delete")) {
            delete(req, resp);
        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) {
        //根据id删除
        //获取前端传递过来的id
        String collogeId = req.getParameter("collogeId");
        //调用service层删除
        collogeService.deleteById(collogeId);
        //删除成功，返回信息给前端
        JSONObject result = new JSONObject();
        result.put("code", 200);
        result.put("msg", "删除成功!");
        resp.setContentType("application/json;charset=UTF-8");
        try {
            PrintWriter writer = resp.getWriter();
            writer.print(JSONObject.toJSONString(result));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    private void editUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取编辑数据的id
        String collogeId = req.getParameter("collogeId");
        //根据Id查询要编辑的数据
        Colloge colloge = collogeService.getById(collogeId);
        //放到request对象里面
        req.setAttribute("colloge", colloge);
        //返回页面给前端
        req.getRequestDispatcher(PREX + "/colloge_edit.jsp").forward(req, resp);
    }

    /**
     * 查询数据列表
     *
     * @param req
     * @param resp
     */
    private void list(HttpServletRequest req, HttpServletResponse resp) {
        //要返回的数据格式
        TableData result = new TableData();
        //封装前端传递过来的参数
        String collogeName = req.getParameter("collogeName");
        Integer page = Integer.valueOf(req.getParameter("page"));
        Integer limit = Integer.valueOf(req.getParameter("limit"));
        //设置参数 CollogeParm
        CollogeParm parm = new CollogeParm();
        parm.setPage(page);
        parm.setLimit(limit);
        parm.setCollogeName(collogeName);
        //表格数据
        List<Colloge> collogeList = collogeService.list(parm);
        //查询总条数
        int count = collogeService.getCount(parm);
        result.setMsg("查询成功");
        result.setCode(0);
        result.setData(collogeList);
        result.setCount(count);
        //返回JSON数据
        resp.setContentType("application/json;charset=UTF-8");
        try {
            PrintWriter writer = resp.getWriter();
            writer.print(JSONObject.toJSONString(result));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 返回学院管理页面
     *
     * @param req
     * @param resp
     */
    private void collogeUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher(PREX + "/colloge.jsp").forward(req, resp);
    }

    /**
     * 返回新增学院页面
     *
     * @param req
     * @param resp
     */
    private void addCollogeUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher(PREX + "/colloge_add.jsp").forward(req, resp);
    }

    /**
     * 新增学院保存
     */
    private void addSave(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //构造携带参数的对象
        Colloge colloge = new Colloge();
        //获取前端传递过来的参数
        String collogeName = req.getParameter("collogeName");
        Integer orderNum = Integer.valueOf(req.getParameter("orderNum"));
        //设置参数到colloge
        colloge.setCollogeName(collogeName);
        colloge.setCollogeId(UUID.randomUUID().toString().replaceAll("-",""));
        colloge.setOrderNum(orderNum);
        collogeService.add(colloge);
        //响应前端数据
        //设置请求头，返回JSON数据
        resp.setContentType("application/json;charset=UTF-8");
        JSONObject result = new JSONObject();
        result.put("code", 200);
        result.put("msg", "新增成功!");
        PrintWriter writer = resp.getWriter();
        writer.print(result.toJSONString());
    }

    /**
     * 编辑保存
     *
     * @param req
     * @param resp
     */
    private void editSave(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取前端传递过来的参数
        String collogeId = req.getParameter("collogeId");
        String collogeName = req.getParameter("collogeName");
        int orderNum = Integer.valueOf(req.getParameter("orderNum"));
        //封装数据
        Colloge colloge = new Colloge();
        colloge.setCollogeId(collogeId);
        colloge.setCollogeName(collogeName);
        colloge.setOrderNum(orderNum);
        //调用更新接口
        collogeService.updateById(colloge);
        //返回数据告诉前端，后台已经更新成功
        JSONObject result = new JSONObject();
        result.put("code", 200);
        result.put("msg", "编辑成功!");
        //把数据写给前端
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.print(result.toJSONString());
    }
}
